import { getProfessions } from '../../api/common_apis'
import { personalBanner } from '@/api/personal_center'
import { Message } from 'element-ui'
export default {
  namespaced: true,
  state: () => ({
    professionList: [],
    centerCurrentView: 'my-course',
    //个人中心顶部广告
    headerAd: {}
  }),
  // 通过 this.$store.commit('a/dispatchCount', 10) 进行操作
  mutations: {
    // 示例：setCount(state, val) {}
    setProfessionList(state, { list }) {
      state.professionList = list
    },
    // 设置当前选择的个人中心组件
    setCenterCurrentView(state, params) {
      state.centerCurrentView = params.name
    },
    // 设置个人中心顶部header
    setHeaderAd(state, params) {
      state.headerAd = params
    }
  },
  // 通过 this.$store.dispatch('a/dispatchCount', 10) 进行操作
  actions: {
    // 示例：getProfessions({ state, commit, rootState }, data) {}
    // 获取专业列表
    async getProfessions({ state, commit, rootState }, params) {
      return getProfessions()
        .then(res => {
          let { errcode, data } = res
          if (+errcode === 0 && Array.isArray(data)) {
            commit('setProfessionList', { list: data })
          }
          return 1
        })
        .catch(e => {
          return 0
        })
    },
    // 获取个人中心当前选中组件
    getCenterCurrentView({ state, commit, rootState }, params) {
      commit('setCenterCurrentView', params)
    },
    // 获取个人中心顶部广告
    async getHeaderAd({ state, commit, rootState }, params) {
      let { data, errcode, errmsg } = await personalBanner().catch(err => {
        return { errocde: 404, errmsg: '网络错误,请重试!', data: [] }
      })
      if (errcode !== 0) {
        Message({ type: 'error', message: errmsg })
      }
      if (Array.isArray(data) && data[0]) {
        commit('setHeaderAd', data[0])
      }
    }
  },
  getters: {}
}
